#随机森林模型的训练和准确率

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np

#拆分数据集
dataset = load_iris()
x, y = dataset.data[:,2:4], dataset.target
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0, test_size=50)

#训练模型
model = RandomForestClassifier(n_estimators=10, random_state=0)
model.fit(x_train, y_train)
#评估模型
pred = model.predict(x_test)
ac = accuracy_score(y_test, pred)
print(f'随机森林模型的预测准确率: {ac}')


#可视化

x1,x2=np.meshgrid(np.linspace(0,8,500),np.linspace(0,3,500))
x_new=np.stack((x1.flat,x2.flat),axis=1)
y_predict=model.predict(x_new)
y_hat=y_predict.reshape(x1.shape)
iris_cmap=ListedColormap(["#ACC6C0","#FF8080","#A0A0FF"])
plt.pcolormesh(x1,x2,y_hat,cmap=iris_cmap)
#绘制3种类别鸢尾花的样本点
plt.scatter(x[y==0,0],x[y==0,1],s=30,c='g',marker='^')
plt.scatter(x[y==1,0],x[y==1,1],s=30,c='r',marker='o')
plt.scatter(x[y==2,0],x[y==2,1],s=30,c='b',marker='s')
#设置坐标轴的名称并显示图形
plt.rcParams['font.sans-serif']='SimHei'
plt.xlabel('花瓣长度')
plt.ylabel('花瓣宽度')
plt.show()